package de.graffiti.paint;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

@SuppressWarnings({"serial", "unused"})
public class JPaintPanel extends JPanel implements MouseMotionListener, MouseListener
{

	private SVGGraphics2D svgGenerator;
	private BufferedImage bufferedImage;
	private Graphics graphics;
	//@SuppressWarnings("unused")
	private Color color = Color.black;

	public void setStroke(int size)
	{
		Graphics2D graph = (Graphics2D) graphics;
		Stroke stroke = new BasicStroke(size);
		graph.setStroke(stroke);
		svgGenerator.setStroke(stroke);

	}

	public void setColor(Color color)
	{
		graphics.setColor(color);
		svgGenerator.setColor(color);
		this.color = color;
	}

	public SVGGraphics2D getSvgGenerator()
	{
		return svgGenerator;
	}

	public JPaintPanel(ImageInfo image)
	{
		super();

		bufferedImage = new BufferedImage(image.getHeight(), image.getWidth(), BufferedImage.TYPE_INT_RGB);

		graphics = bufferedImage.getGraphics();
		graphics.setColor(Color.white);

		graphics.setColor(Color.black);

		addMouseListener(this);
		addMouseMotionListener(this);

		// Get a DOMImplementation.
		DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();

		// Create an instance of org.w3c.dom.Document.
		String svgNS = "http://www.w3.org/2000/svg";
		Document document = domImpl.createDocument(svgNS, "svg", null);
		// Create an instance of the SVG Generator.
		svgGenerator = new SVGGraphics2D(document);

		Dimension dim = new Dimension(image.getHeight(), image.getWidth());
		svgGenerator.setSVGCanvasSize(dim);

		graphics.drawImage(image.getPicture(), 0, 0, this);

	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawImage(bufferedImage, 0, 0, 100, 100, this);
	}

	public void repaint1()
	{
		this.getGraphics().drawImage(bufferedImage, 0, 0, 100, 100, this);
	}

	public void mouseDragged(MouseEvent arg0)
	{
		Point p = arg0.getPoint();

		graphics.drawLine(lastPos.x, lastPos.y, p.x, p.y);
		svgGenerator.drawLine(lastPos.x, lastPos.y, p.x, p.y);
		repaint();
		lastPos = p;

	}

	public void mousePressed(MouseEvent arg0)
	{
		lastPos = arg0.getPoint();
	}

	public void mouseReleased(MouseEvent arg0)
	{
		lastPos = null;
	}

	public void mouseMoved(MouseEvent arg0)
	{
	}

	public void mouseClicked(MouseEvent arg0)
	{
	}

	public void mouseEntered(MouseEvent arg0)
	{
	}

	public void mouseExited(MouseEvent arg0)
	{
	}

	protected Point lastPos = null;

}
